package ysq.xyg.web.controller;

import cn.hutool.core.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.model.po.WebConfig;
import ysq.xyg.web.model.vo.WebConfigVo;
import ysq.xyg.web.service.WebConfigService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class WebConfigController extends BaseController {

    @Autowired
    private WebConfigService webConfigService;

    @RequestMapping(value = "/webConfig/getEntity", method = RequestMethod.GET)
    public ModelAndView getEntity(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/web/webConfigUpdateOrSave");
        WebConfig search = new WebConfig();
        search.setAccountId(acceptAccountId(request));
        search.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
        List<WebConfig> webConfigs = webConfigService.select(search);
        if (CollUtil.isNotEmpty(webConfigs)) {
            view.addObject("webConfig", webConfigs.get(0));
        }
        return view;
    }

    @RequestMapping(value = "/webConfig/addOrUpdate", method = RequestMethod.POST)
    public ReturnEntity delete(HttpServletRequest request, WebConfigVo webConfigVo){
        return webConfigService.addOrUpdate(webConfigVo, acceptCurrentUser(request));
    }
}